package com.bxj.mybatis.generator.plugin;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.List;
import java.util.Properties;

public class CleanPlugin extends PluginAdapter {


    private String targetProject;
    private String targetPackage;

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        targetProject = properties.getProperty("targetProject"); //$NON-NLS-1$
        targetPackage = properties.getProperty("targetPackage"); //$NON-NLS-1$
    }

    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
            IntrospectedTable introspectedTable) {
        DefaultShellCallback callback = new DefaultShellCallback(true);
        try {
            File directory = callback.getDirectory(targetProject, targetPackage);
            for (File mapper : directory.listFiles()) {
                mapper.delete();
            }
        } catch (ShellException e) {
            e.printStackTrace();
        }
        return null;
    }

}
